package com.ebt.m.proposal_v2.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.ebt.m.R;
import com.ebt.m.proposal_v2.dao.response.ResponseBrand;
import com.ebt.m.proposal_v2.widget.view.RadioCell;

import java.util.List;

/**
 * 品牌项
 *
 * @author archer.qi
 *         Created on 2017/5/25.
 */
public class BrandAdapter extends BaseAdapter {

    private Context mContext;
    private List<ResponseBrand> data;
    private LayoutInflater inflater;

    private ResponseBrand mChecked;

    public BrandAdapter(Context context, List<ResponseBrand> data) {
        mContext = context;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    public void setChecked(ResponseBrand check) {
        mChecked = check;
        notifyDataSetChanged();
    }


    private boolean isChecked(int position) {
        if (data == null && position > data.size() - 1 || mChecked == null) {
            return false;
        }
        return mChecked.brandId == data.get(position).brandId;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_grid_brand, null);
            holder = new GridViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GridViewHolder) convertView.getTag();
        }
        holder.setContent(data.get(position).brandShortName);
        holder.setChecked(isChecked(position));
        return convertView;
    }

    private class GridViewHolder {
        public View itemView;
        public RadioCell labelView;

        public GridViewHolder(View itemView) {
            this.itemView = itemView;
            initLabelView();
        }

        private void initLabelView() {
            labelView = (RadioCell) itemView.findViewById(R.id.view_label);
        }

        public void setChecked(boolean isChecked) {
            labelView.updateCheck(isChecked);
        }

        public void setContent(String content) {
            labelView.setText(content == null ? "" : content);
        }
    }
}
